package com.innovation.chengzhangriji.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.ConfigurationManager;
import com.innovation.library.util.InnovationAlgorithm;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.http.SignUpResponse;
import com.innovation.chengzhangriji.ui.animation.AnimationConfig;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteEditGroup;
import com.umeng.analytics.MobclickAgent;

public class SignUp3Fragment extends BaseFragment implements View.OnClickListener {

    private static final int PASSWORD_MIN_LENGTH = 6;

    private static final int PASSWORD_MAX_LENGTH = 16;

    private static final CharSequence SPACE = " ";

    public interface SignUpListener {

        /**
         * 注册成功
         */
        void signUpSuccess(SignUpResponse response, String phone);

    }

    private SignUpListener mSignUpListener;

    private String phone;

    private String code;

    private CuteEditGroup mCegPassword1;

    private CuteEditGroup mCegPassword2;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mSignUpListener = (SignUpListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement SignUpListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        phone = getArguments().getString(SignUpActivity.PHONE);
        code = getArguments().getString(SignUpActivity.CODE);
        View view = inflater.inflate(R.layout.fragment_signup_3, container, false);
        mCegPassword1 = (CuteEditGroup) view.findViewById(R.id.edit_text_password_1);
        mCegPassword2 = (CuteEditGroup) view.findViewById(R.id.edit_text_password_2);
        CuteButton button = (CuteButton) view.findViewById(R.id.btn_complete);
        button.setOnClickListener(this);
        return view;
    }

    @Override
    protected void onPageStart() {
        MobclickAgent.onPageStart(Config.UMENG_PAGE_SIGN_3);
    }

    @Override
    protected void onPageEnd() {
        MobclickAgent.onPageEnd(Config.UMENG_PAGE_SIGN_3);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_complete) {
            String password = mCegPassword2.getContent();
            if (checkPasswordDifferent(password)) {
                requestSignUp(phone, code, InnovationAlgorithm.SHA1(Config.getConfig(ConfigurationManager.PASSWORD_SALT), password));
            }
        }
    }

    public void setSignUpListener(SignUpListener signUpListener) {
        mSignUpListener = signUpListener;
    }

    private boolean checkPassword(String content, View view) {
        if (TextUtils.isEmpty(content) || content.length() < PASSWORD_MIN_LENGTH || content.length() > PASSWORD_MAX_LENGTH) {
            AnimationConfig.shake(getActivity(), view);
            tipShow(R.string.tip_error_password);
            return false;
        }

        if (content.contains(SPACE)) {
            AnimationConfig.shake(getActivity(), view);
            tipShow(R.string.tip_error_password_empty);
            return false;
        }
        return true;
    }

    private boolean checkPasswordDifferent(String content) {
        if (!checkPassword(content, mCegPassword1)) {
            return false;
        }

        if (TextUtils.isEmpty(mCegPassword1.getContent()) || !mCegPassword1.getContent().equals(content)) {
            AnimationConfig.shake(getActivity(), mCegPassword2);
            tipShow(R.string.tip_error_password_different);
            return false;
        }
        return true;
    }

    private void requestSignUp(final String phone, String code, String password) {
        Client.requestSignUp(getActivity(), phone, code, password, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                dismissProgressDialog();
                SignUpResponse response = get(value.toString(), SignUpResponse.class);
                if (mSignUpListener != null) {
                    mSignUpListener.signUpSuccess(response, phone);
                }
            }

            @Override
            public void onChZhRJFailure(String value) {
                dismissProgressDialog();
                tipShow(value);
            }

        }, networkUnavailableSampleImpl());
    }
}
